<?php
namespace Home\Controller;
use Think\Controller;
class OpenController extends Controller {


   //定时任务返还的访问 每周定时访问
    public function week()
    {     
            //$cur_time=time();
                     $cur_time =strtotime('2016-09-25');
  $moshi=M('moshi')->where('status=1')->find();
            //下单之后返还财富积分
            $orderLists=M('order')->where('paystatus=1')->select();
              //$cond['status']=array('EQ','NULL');  
           /* $order_fhs=M('order_fh')->where('status=0')->select();
            foreach ($order_fhs as $key => $value) {
            

               if ($cur_time>=$value['hf_endtime'] && $cur_time<=($value['hf_endtime']+86399)) {
                     
               }
            }
            exit();*/


            foreach ($orderLists as $key => $value) {
                        $start=$value['begin_hf_time'];
                        $mid=$value['week1end_hf_time']+86399;
                        $end=$value['end_hf_time']+86399; 

                         if ($cur_time>=$start && $cur_time<=$mid){
                            $add_cf=$value['moshi_cf_week1']*$value['order_num'];
                            $add_xf=$value['moshi_xf_week1']*$value['order_num'];
                            M('order')->where("id=".$value['id'])->setInc('moshi_yfh_cf',$add_cf);
                            M('order')->where("id=".$value['id'])->setInc('moshi_week1_fh_num');
                            M('user')->where("id=".$value['user_id'])->setInc('cf_jifen',$add_cf);
                            M('user')->where("id=".$value['user_id'])->setInc('xf_jifen',$add_xf);
             $record['user_id']=$value['user_id'];
             $record['items']='每周返还财富积分';
             $record['jifen_num']='+'.$add_cf;
             $record['addtime']=time();
             $record['jifen_type']='cf';
             M('jifen_record')->add($record);

             if ($add_xf) {
                 $record['user_id']=$value['user_id'];
             $record['items']='每周返还财富积分';
             $record['jifen_num']='+'.$add_xf;
             $record['addtime']=time();
             $record['jifen_type']='xf';
             M('jifen_record')->add($record);
             }
    

             $fh_map['order_id']=$value['id'];
            $fh_map['hf_endtime']  = array('between',array($cur_time,$cur_time+86399));
             $fh['cf_jifen']=$add_cf;
             $fh['status']=1;
             M('order_fh')->where($fh_map)->save($fh);
                         }
             
                         if ($cur_time>=$mid && $cur_time<=$end) {
                        unset($add_cf);
                            $add_cf=$value['moshi_cf_week2']*$value['order_num'];
                            $add_xf=$value['moshi_xf_week2']*$value['order_num'];

                            M('user')->where("id=".$value['user_id'])->setInc('cf_jifen',$add_cf);
                            M('user')->where("id=".$value['user_id'])->setInc('xf_jifen',$add_xf);
                            M('order')->where("id=".$value['id'])->setInc('moshi_yfh_cf',$add_cf);
                            M('order')->where("id=".$value['id'])->setInc('moshi_yfh_xf',$add_xf);
                            M('order')->where("id=".$value['id'])->setInc('moshi_week2_fh_num');

                            unset($record);
                             $record['user_id']=$value['user_id'];
             $record['items']='每周返还财富积分';
             $record['jifen_num']='+'.$add_cf;
             $record['addtime']=time();
             $record['jifen_type']='cf';
             M('jifen_record')->add($record);

             $record2['user_id']=$value['user_id'];
             $record2['items']='每周返还消费积分';
             $record2['jifen_num']='+'.$add_xf;
             $record2['addtime']=time();
             $record2['jifen_type']='xf';
             M('jifen_record')->add($record2);

              $fh_map['order_id']=$value['id'];
            $fh_map['hf_endtime']  = array('between',array($cur_time,$cur_time+86399));
             $fh['cf_jifen']=$add_cf;
             $fh['xf_jifen']=$add_cf;
             $fh['status']=1;
             M('order_fh')->where($fh_map)->save($fh);

                         }

                        if ($cur_time >$end) {
                        $fund=M('sys')->getField('fund');
                        M('sys')->setInc('fund_total',$fund);
                        $data['fund_num']=$fund;
                        $data['addtime']=time();
                        $data['user_id']=$value['user_id'];
                        $data['items']='增加公益基金';

                        M('fund_record')->add($data);
                        }


            }

            $order_gz= M('order_gz')->where('gz_status>0')->select();

            foreach ($order_gz as $key => $value) {
                # code...
            }





    }

     //满10单之后返还财富积分的逻辑
  /*  public function fahuan_cf()
    {       
        $cur_time=time();
        unset($map);
        $map['gz_status']  = array('lt',12);
            $order_gz= M('order_gz')->where($map)->select();
            $addcf=M('jifen')->getField('jifen_cf_gz');
            $addcf=ceil($addcf/12);

            foreach ($order_gz as $key => $value) {
             
                    for ($i=1; $i <2; $i++) { 
                      $month[$i]=strtotime("+".$i."months", $value['addtime']);
                      $start = mktime(0,0,0,date("m",$month[$i]),date("d",$month[$i]),date("Y",$month[$i]));
                      $end = mktime(23,59,59,date("m",$month[$i]),date("d",$month[$i]),date("Y",$month[$i]));
                        if ($cur_time>=$start && $cur_time <$end) {
                                M('user')->where(array('id'=>$value['user_id']))->setInc('cf_jifen',$addcf);
                                M('order_gz')->where(array('id'=>$value['id']))->setInc('gz_status',1);

                            }


                    }
                 
            }

    }
*/
    //每天的定时任务
    public function day_exe()
    {
         $cur_time=time();
            //下单之后返还财富积分 

            /*$gz=M('order_gz')->find();
            if ($gz) {
              $this->fahuan_cf();
            }*/

                   $firstday = date("Y-m-01",$cur_time);
      $lastdayend = date("Y-m-d 23:59:59",strtotime("$firstday +1 month -1 day"));
      $lastdaybegin = date("Y-m-d 00:00:01",strtotime("$firstday +1 month -1 day"));

      if ($cur_time >=$lastdaybegin && $cur_time <= $lastdayend) {
             $cond['beyond_sj_id']=array('NEQ','NULL');   
             $list=M('user')->where($cond)->select();
             if ($list) {
                foreach ($list as $key => $value) {
                    if ($cur_time <= $value['beyond_end_time'] ) {
                    $map['addtime'] = array(array('egt',$lastdaybegin),array('elt',$lastdayend),'AND');
                    $map['user_id'] =$value['id'] ;
                    $totalnum=M('order_team')->where($map)->sum('order_num');
                    M('user')->where('id='.$value['beyond_sj_id'])->setInc('cf_jifen',$totalnum*0.01);
                    }
                }
             }
        }
    }



}
